#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void ChildRun(){
  int cnt = 5;
while(1){//无限循环，测试返回信号
  printf("This is child process,pid: %d,ppid: %d,cnt: %d\n",getpid(),getppid(),cnt);
  sleep(1);
  cnt--;
  }
}

int main(){
  printf("I am father process,pid: %d,ppid: %d\n",getpid(),getppid());
  pid_t id = fork();
  if(id==0){
    //child
    ChildRun();
    printf("child is quit...\n");
    exit(123);
  }
  sleep(7);
  //father
  // pid_t rid=wait(NULL);
  int status=0;
  pid_t rid=waitpid(id,&status,0); //等同于上
  //pid_t rid=waitpid(id+1,NULL,0); //错误等待
  if(rid>0){
    printf("wait is Sussess!!! rid: %d\n",rid);

  }
  else{
    printf("wait faild!!\n");
  }
  sleep(3);
  printf("father is quit....,status: %d,child quit code: %d,child quit singal:%d\n",status,(status>>8)&0xFF,status&0x7F);//返回退出码和退出信号。
}
